package com.twu.biblioteca;

import java.io.PrintStream;
import java.util.*;

public class Library {
    private Map<String,Book> bookList;
    private Map<String,Book> checkedBooks = new TreeMap<String, Book>();
    private PrintStream printStream;
    private StringJoiner joiner;


    public Library(PrintStream printStream, StringJoiner joiner, Map<String, Book> bookList) {
        this.printStream = printStream;
        this.joiner = joiner;
        this.bookList = bookList;
    }

    public Map<String,Book> getAvailableBooks() {
        return bookList;
    }

    public Map<String,Book> getCheckedBooks() {
        return checkedBooks;
    }

    public boolean returnBook(String bookId) {
        if (checkedBooks.keySet().contains(bookId)) {
            bookList.put(bookId, checkedBooks.get(bookId));
            checkedBooks.remove(bookId);
            return true;
        }
        return false;
    }

    public boolean checkout(String bookId) {
        if (bookList.keySet().contains(bookId)) {
            checkedBooks.put(bookId, bookList.get(bookId));
            bookList.remove(bookId);
            return true;
        }
        return false;
    }

    public void listBooks() {
        String joinedBooks = joiner.join(bookList);
        printStream.println(joinedBooks);
    }
}
